<template>
  <div class="home">
      <div class="home-put">
          <input v-model="lotCode" type="text" placeholder="请输入优惠券码">
          <button @click="clickTest">点击校验</button>
      </div>
      <van-popup v-model="show">
          <div class="home-confirm">
              <p>恭喜您核验成功!</p>
              <button @click="clickTarget">进去领取游戏币</button>
          </div>
      </van-popup>
  </div>
</template>
<script>
import lottor from "api/lottor";
import { Toast, Popup } from "vant";
import Vue from "vue";
Vue.use(Popup);

export default {
  name: "home",
  data() {
    return {
      lotCode: "",
      show: false
    };
  },
  components: {
    Popup
  },
  methods: {
    clickTest() {
      const reg = /^[a-zA-Z0-9]{6}$/;
      if (!reg.test(this.lotCode)) {
        Toast.fail("请输入正确的券码");
        return;
      }
      lottor(this.lotCode)
        .then(res => {
          const { data } = res;
          if (data.length > 0) {
            if (data[0].status == 1) {
              let loopFlag = window.localStorage.getItem("loopFlag") || "0";
              if (loopFlag < 19) {
                window.localStorage.setItem("loopFlag", parseInt(loopFlag) + 1);
              } else {
                window.localStorage.setItem("loopFlag", "0");
              }
              this.show = true;
              return;
            }
          }
          Toast.fail("优惠券无效或者已被核验");
        })
        .catch(err => {
          Toast.fail("服务器异常，亲稍后再试！");
        });
    },
    clickTarget() {
      this.$router.push("/from");
      this.show = false;
    }
  },
  created(){
    document.title='肯德基娃娃机体验券核销页'
  }
};
</script>
<style scoped lang="scss">
.home {
  position: relative;
  height: 100%;
  width: 100%;
  background: url('../common/img/l-bg.jpg')no-repeat center/cover;
  &-put {
    position: absolute;
    top: 60%;
    left: 50%;
    transform: translate(-50%, -50%);
    input {
      height: 2rem;
      width: 12rem;
      box-sizing: border-box;
      // padding-left: 1rem;
      border: 1px solid #c1c1c1;
      border-radius: 0.2rem;
      text-align: center;
      &::-webkit-input-placeholder {
        /* WebKit browsers */
        font-size: 0.8rem;
        // line-height: 1rem;
        text-align: center;
      }
    }
    button {
      height: 2rem;
      width: 12rem;
      border-radius: 0.2rem;
      margin-top: 1rem;
      box-sizing: border-box;
      background-color: #f8e2a7;
      font-weight: bold;
      color: #a40000;
      font-size: .9rem;
      // border: 1px solid #fff;
    }
  }

  &-confirm {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    line-height: 1rem;
    background-color: #f8e2a7;
    width: 12.5rem;
    height: 8rem;
    border-radius: 0.3rem;
    font-size: 0.6rem;
    p{
      color: #a40000;
      font-size: .9rem
    }
    button {
      width: 80%;
      height: 2rem;
      border-radius: 0.3rem;
      background-color: #a40000;
      color: white;
      font-weight: bold;
      font-size: .7rem;
    }
  }
}
.van-popup {
  background-color: transparent;
}
</style>
